package com.maiji.cloud.controller;

import com.maiji.cloud.entities.shopingmall.GoodsComment;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.shopingmall.GoodsCommentFindAllReqData;
import com.maiji.cloud.request.shopingmall.FindByIdReqDto;
import com.maiji.cloud.request.shopingmall.GoodsCommentReqData;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.GoodsCommentCommentService;
import com.maiji.cloud.service.GoodsCommentService;
import com.maiji.cloud.utils.WXUtil;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 *  商品评论操作相关请求
 */
@RestController
@RequestMapping("/goodsComment/")
public class GoodsCommentController {

    @Autowired
    private GoodsCommentService goodsCommentService;
    @Autowired
    private GoodsCommentCommentService goodsCommentCommentService;

    /**
     *  统计未审核商品评论数量
     * @return
     */
    @GetMapping("goodsCommentShieldCount")
    public BaseDataResDto<Integer> GoodsCommentShieldCount() {
        return goodsCommentService.GoodsCommentShieldCount();
    }

    /**
     *  获取商品评论列表
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findAllGoodsComment")
    public BaseMetaResDto<List<GoodsComment>> findAllGoodsComment(@RequestBody BaseDataReqDto<GoodsCommentFindAllReqData> baseDataReqDto,
                                                                  @RequestHeader("maijiToken") String token) {
        String goodsId = baseDataReqDto.getData().getGoodsId();
        ReqMetaData reqMetaData = baseDataReqDto.getMetaData();
        if (goodsId == null) return new BaseMetaResDto(Status.PARAMETERERROR);
        return goodsCommentService.findAllGoodsComment(baseDataReqDto.getData(), reqMetaData, token);
    }

    /**
     *  查看商品评论详情
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("findGoodsCommentById")
    public BaseMetaResDto<GoodsComment> findGoodsCommentById(@RequestBody BaseDataReqDto<String> baseDataReqDto, @RequestHeader("maijiToken") String token) {
        String goodsCommentId = baseDataReqDto.getData();
        if (goodsCommentId == null) return new BaseMetaResDto(Status.PARAMETERERROR);
        return goodsCommentService.findGoodsCommentById(goodsCommentId, token);
    }

    /**
     *  商品评论的点赞或取消点赞
     * @param findByIdReqDto
     * @param token
     * @return
     */
    @PostMapping("thumbsUpGoodsCommentById")
    public BaseResDto thumbsUpGoodsCommentById(@RequestBody FindByIdReqDto findByIdReqDto, @RequestHeader("maijiToken") String token) {
        String goodsCommentId = findByIdReqDto.getData();
        if (goodsCommentId == null) return new BaseResDto(Status.PARAMETERERROR);
        return goodsCommentService.thumbsUpGoodsCommentById(goodsCommentId, token);
    }

    /**
     *  发表商品评论
     * @param baseDataReqDto
     * @param token
     * @return
     */
    @PostMapping("publishGoodsComment")
    public BaseResDto publishGoodsComment(@RequestBody BaseDataReqDto<GoodsCommentReqData> baseDataReqDto,
                                              @RequestHeader("maijiToken") String token) {
        String orderId = baseDataReqDto.getData().getOrderId();
        String goodsId = baseDataReqDto.getData().getGoodsId();
        String content = baseDataReqDto.getData().getContent();
        Integer star = baseDataReqDto.getData().getStar();
        List<String> commentLabelIds = baseDataReqDto.getData().getCommentLabelIds();
        List<String> commentImageIds = baseDataReqDto.getData().getCommentImageIds();
        if (StringUtil.isBlank(orderId) || StringUtil.isBlank(goodsId)) return new BaseResDto(Status.PARAMETERERROR);
        if (! WXUtil.examineText(content)) return new BaseResDto(Status.NON_COMPLIANCE);
        return goodsCommentService.publishGoodsComment(orderId, goodsId, content, star, commentLabelIds, commentImageIds, token);
    }

    /**
     *  批量发表商品评论
     * @param baseDataReqDto
     * @param token
     * @return
     */
    @PostMapping("publishGoodsCommentBatch")
    public BaseResDto publishGoodsCommentBatch(@RequestBody BaseDataReqDto<List<GoodsCommentReqData>> baseDataReqDto,
                                              @RequestHeader("maijiToken") String token) {
        BaseResDto baseResDto = new BaseResDto();
        List<GoodsCommentReqData> data = baseDataReqDto.getData();
        if (data == null || data.size() == 0) return baseResDto.setStatusR(Status.PARAMETERERROR);
        for (GoodsCommentReqData gc: data) {
            String orderId = gc.getOrderId();
            String goodsId = gc.getGoodsId();
            String content = gc.getContent();
            Integer star = gc.getStar();
            List<String> commentLabelIds = gc.getCommentLabelIds();
            List<String> commentImageIds = gc.getCommentImageIds();
            if (StringUtil.isBlank(orderId) || StringUtil.isBlank(goodsId)) return baseResDto.setStatusR(Status.PARAMETERERROR);
            baseResDto = goodsCommentService.publishGoodsComment(orderId, goodsId, content, star, commentLabelIds, commentImageIds, token);
            if (baseResDto.getStatus().getValue() != 1) return baseResDto;
        }
        return baseResDto;
    }

    /**
     *  删除商品评论
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("delGoodsComment")
    public BaseResDto delGoodsComment(@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String gcId = baseDataReqDto.getData();
        if (StringUtil.isBlank(gcId)) return new BaseResDto(Status.PARAMETERERROR);
        return goodsCommentService.delGoodsComment(gcId);
    }

    /**
     *  屏蔽/取消屏蔽商品评论
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("shieldGoodsComment")
    public BaseResDto shieldGoodsComment(@RequestBody BaseDataReqDto<GoodsCommentFindAllReqData> baseDataReqDto) {
        String goodsId = baseDataReqDto.getData().getGoodsId();
        String gcId = baseDataReqDto.getData().getGcId();
        Integer shield = baseDataReqDto.getData().getShield();
        if (StringUtil.isBlank(goodsId) || StringUtil.isBlank(gcId) || shield == null) return new BaseResDto(Status.PARAMETERERROR);
        return goodsCommentService.shieldGoodsComment(goodsId, gcId, shield);
    }

    /**
     *  回复评论
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("replyGoodsComment")
    public BaseResDto replyGoodsComment(@RequestBody BaseDataReqDto<GoodsCommentFindAllReqData> baseDataReqDto,
                                        @RequestHeader("maijiToken") String token) {
        String gcId = baseDataReqDto.getData().getGcId();
        String content = baseDataReqDto.getData().getContent();
        if (StringUtil.isBlank(gcId) || StringUtil.isBlank(content)) return new BaseResDto(Status.PARAMETERERROR);
        if (! WXUtil.examineText(content)) return new BaseResDto(Status.NON_COMPLIANCE);
        return goodsCommentCommentService.replyGoodsComment(gcId, content, token);
    }

}

